package com.ctakit.sdk.app.util;

import android.content.Context;
import android.content.SharedPreferences;

import com.ctakit.sdk.SdkApplication;

import net.grandcentrix.tray.AppPreferences;

public class SharedPreferenceUtil {
    private static AppPreferences appPreferences = null;// this Preference comes for free from the library


    private static SharedPreferences preferences;

    private static synchronized SharedPreferences getSharedPreferences() {
        if (preferences == null) {
            preferences = SdkApplication.app().getSharedPreferences("setting", 0);
        }
        return preferences;
    }


    private static synchronized AppPreferences getAppSharedPreferences() {
        if (appPreferences == null) {
            try {
                appPreferences = new AppPreferences(SdkApplication.app());
            } catch (Exception e) {

            }

        }

        return appPreferences;
    }

    public static void putStringValueByKey(String key,
                                           String value) {
        try {
            getAppSharedPreferences().put(key, value);
            getSharedPreferences().edit().putString(key, value).commit();

        } catch (Exception e) {
            getSharedPreferences().edit().putString(key, value).commit();
            e.printStackTrace();
        }
    }

    public static String getString(String key, String defaultValue) {
        String value = null;
        try {
            value = getAppSharedPreferences().getString(key, defaultValue);

        } catch (Exception e) {
            return getSharedPreferences().getString(key, defaultValue);
        }
        return value;
    }

    public static void putLongValueByKey(String key, long value) {
        try {
            getAppSharedPreferences().put(key, value);
            getSharedPreferences().edit().putLong(key, value).commit();

        } catch (Exception e) {
            getSharedPreferences().edit().putLong(key, value).commit();
            e.printStackTrace();
        }
    }

    public static long getLong(String key, long defaultValue) {
        Long value = null;
        try {
            value = getAppSharedPreferences().getLong(key, defaultValue);

        } catch (Exception e) {
            return getSharedPreferences().getLong(key, defaultValue);
        }
        return value;
    }

    public static void putIntValueByKey(String key, int value) {
        try {
            getAppSharedPreferences().put(key, value);
            getSharedPreferences().edit().putInt(key, value).commit();

        } catch (Exception e) {
            getSharedPreferences().edit().putInt(key, value).commit();
            e.printStackTrace();
        }
    }

    public static int getIntValueByKey(String key,
                                       int defaultValue) {
        Integer value = null;
        try {
            value = getAppSharedPreferences().getInt(key, defaultValue);

        } catch (Exception e) {
            return getSharedPreferences().getInt(key, defaultValue);
        }
        return value;
    }

    public static void putFloatValueByKey(String key, float value) {
        try {
            getAppSharedPreferences().put(key, value);
            getSharedPreferences().edit().putFloat(key, value).commit();

        } catch (Exception e) {
            getSharedPreferences().edit().putFloat(key, value).commit();
            e.printStackTrace();
        }
    }

    public static float getFloatValueByKey(String key, int defaultValue) {
        Float value = null;
        try {
            value = getAppSharedPreferences().getFloat(key, defaultValue);

        } catch (Exception e) {
            return getSharedPreferences().getFloat(key, defaultValue);
        }
        return value;
    }

    public static void putBooleanByKey(String key, boolean value) {
        try {
            getAppSharedPreferences().put(key, value);
            getSharedPreferences().edit().putBoolean(key, value).commit();

        } catch (Exception e) {
            getSharedPreferences().edit().putBoolean(key, value).commit();
            e.printStackTrace();
        }
    }

    public static boolean getBooleanByKey(String key, boolean defaultValue) {
        Boolean value = null;
        try {
            value = getAppSharedPreferences().getBoolean(key, defaultValue);

        } catch (Exception e) {
            return getSharedPreferences().getBoolean(key, defaultValue);
        }
        return value;
    }
}